package org.linlinjava.litemall.admin.service;

import java.util.List;

import org.linlinjava.litemall.admin.erp.client.MaterialClient;
import org.linlinjava.litemall.admin.erp.rq.MaterialRQ;
import org.linlinjava.litemall.admin.erp.rs.MaterialRS;
import org.linlinjava.litemall.admin.erp.rs.PageModelRS;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallMerchandise;
import org.linlinjava.litemall.db.service.LitemallMerchandiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 管理erp服务
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Service
public class AdminErpService {
	/**
	 * 商品服务
	 */
	@Autowired
    private LitemallMerchandiseService merchandiseService;

	@Autowired
	private AdminLumiereMaterialClassificationService adminLumiereMaterialClassificationService;

	@Autowired
	private AdminLumiereFormulaMaterialService adminLumiereFormulaMaterialService;
	/**
	 * 材料客户
	 */
	@Autowired
    private MaterialClient materialClient;

	/**
	 * 同步货品
	 *
	 * @param apiName         api名称
	 * @param materialListUrl 材料清单url
	 * @return {@link Object}
	 */
	public Object sync(String apiName, String materialListUrl){

    	Integer page = 1;
    	Integer limit = 20;
    	Integer totalPage = 0;
    	
        MaterialRQ rq = new MaterialRQ();
        rq.setApiName(apiName);
        rq.setPageSize(limit);
    	do {
    		rq.setCurrentPage(page);
    		PageModelRS<MaterialRS> materialPage = materialClient.getMaterialList(rq, materialListUrl);
    		List<MaterialRS> materialList =  materialPage.getRows();
    		if(CollectionUtils.isEmpty(materialList)) {
    			break;
    		}
    		
    		for (MaterialRS materialRS : materialList) {
				//处理原料
    			String barCode = materialRS.getBarCode();
    			//LitemallMerchandise merchandise = merchandiseService.queryBySn(barCode);
				LitemallMerchandise merchandise = merchandiseService.findByName(materialRS.getName());
    	    	if(null == merchandise) {
    	    		merchandise = new LitemallMerchandise();
    	    		merchandise.setMerchandiseSn(barCode);
    	    		setLitemallMerchandiseProps(merchandise, materialRS);
    	    		merchandiseService.create(merchandise);
    	    	}else {
    	    		setLitemallMerchandiseProps(merchandise, materialRS);
    	    		merchandiseService.updateById(merchandise);
    	    	}
			}
    		
    		if(page == 1) {
    			int total = materialPage.getTotal().intValue();
    			if(total % limit == 0) {
    				totalPage = total/limit;
    			}else {
    				totalPage = total/limit + 1;
    			}
    		}
    		page++;
		} while (page <= totalPage);
		adminLumiereMaterialClassificationService.syncLumiereMaterialClassifications();
		//同步原料配方
		adminLumiereFormulaMaterialService.syncLumiereFormulaMaterial();
        return ResponseUtil.ok();
    }

	/**
	 * 设置litemall商品道具
	 *
	 * @param merchandise 商品
	 * @param materialRS  材料rs
	 */
	private void setLitemallMerchandiseProps(LitemallMerchandise merchandise, MaterialRS materialRS) {
    	merchandise.setName(materialRS.getName());
    	merchandise.setBasicUnit(materialRS.getBasicUnit());
    	merchandise.setCheckUnit(materialRS.getCheckUnit());
    	merchandise.setUnit(materialRS.getOtherUnit());
    	merchandise.setCheckRatio(materialRS.getCheckRatio());
		merchandise.setClassification(materialRS.getCategoryName());
    	merchandise.setRatio(materialRS.getRatio());
    	merchandise.setPurchasePrice(materialRS.getWholesaleDecimal());
    	merchandise.setModel(materialRS.getModel());
    	merchandise.setStandard(materialRS.getStandard());
    	merchandise.setNumber(materialRS.getStock().doubleValue());
    	merchandise.setPicUrl(materialRS.getPicUrl());
    }

    
}
